<?php

namespace app\storeapi\model\dealer;
use think\Db;
use app\common\model\DistributionSetting as DistributionSettingModel;


/**
 * 分销商提现明细模型
 * Class Withdraw
 * @package app\store\model\dealer
 */
class DistributionSetting extends DistributionSettingModel
{
    /**
     * 添加分销方案一
     * @param string $search
     * @return mixed|\think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setOne($param)
    {

        if (!isset($param['distribution_type']) || empty($param['distribution_type'])) {
            $this->error = '请选择分销类型';
            return false;
        }

        $updateData =  [
            'distribution_con'=>$param['distribution_con'],//分销条件
            'distribution_type'=>$param['distribution_type'],//分销类型  1：单笔  2：累加
            'consume_charge'=>$param['consume_charge'],//消费赏金百分比
            'product_status'=>$param['product_status'],//商品赏金  1：开启 2：关闭
            'card_status'=>$param['card_status'],//储值卡赏金状态 1：开启  2：关
            'hot_sale_status'=>$param['hot_sale_status'],//爆品礼赏金状态  1：开启 2：关闭
            'status'=>$param['status'],//配置状态  1：开启 2：关闭
        ];
        $setting_content = json_encode($updateData,256);
        $data  = [
            'wxapp_id'=>$param['wxapp_id'],
            'dealer_type'=>1,
            'setting_content'=>$setting_content,
        ];
        // 开启事务
        $this->startTrans();
        try {
            Db::name('wxapp')->where(['wxapp_id'=>$param['wxapp_id']])->update(['dealer_case'=>$param['dealerCase']]);
            $this->allowField(true)->save($data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }

    }





    /**
     * 添加分销方案二
     * @param string $search
     * @return mixed|\think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setTow($param)
    {

        $updateData =  [
            'common' => $param['common'],
            'staff' => $param['staff'],
            'wgd' => $param['wgd'],
            'vip' => $param['vip'],
        ];

        $setting_content = json_encode($updateData,256);
        $data  = [
            'wxapp_id'=>$param['wxapp_id'],
            'dealer_type'=>2,
            'setting_content'=>$setting_content,
        ];
        // 开启事务
        $this->startTrans();
        try {
            Db::name('wxapp')->where(['wxapp_id'=>$param['wxapp_id']])->update(['dealer_case'=>$param['dealerCase']]);
            $this->allowField(true)->save($data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }

    }



}